package com.egoo.ticket.server.controller;

import com.egoo.ticket.server.pojo.entity.TaskSignIn;
import com.egoo.ticket.server.pojo.vo.TaskSignInVo;
import com.egoo.ticket.server.service.TaskSignInService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author：victor_tang
 * @Date ：2020/2/17
 * @Description：任务签到控制器
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1/taskSignIn", produces = "application/json")
@Api(value = "任务签到管理",tags = "任务签到管理",description = "任务签到管理")
public class TaskSignInController {

	@Autowired
	private TaskSignInService taskSignInService;

	@ApiOperation(value = "根据流程实例id获取当前任务签到信息", notes = "根据流程实例id获取当前任务签到信息")
	@RequestMapping(method = RequestMethod.GET, value = "/getTaskSignInByProcessInstanceId")
	public DbResponse getTaskSignInByProcessInstanceId(@ApiParam(value =
			"流程实例id", required = true) @RequestParam(name = "processInstanceId") String processInstanceId,
													   HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (StringUtils.isEmpty(processInstanceId)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"流程实例id不能为空！");
		}
		try {
			TaskSignInVo taskSignInVo =
					taskSignInService.getTaskSignInByProcessInstanceId(processInstanceId,tenantId);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					ResponseConstant.SELECT_SUCCESS_MSG,taskSignInVo);
		} catch (Exception e) {
			log.error("根据流程实例id获取当前任务签到信息异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "根据流程实例id获取当前任务签到信息异常："+e);
		}
	}
	@ApiOperation(value = "新增或者更新任务签到信息", notes = "新增或者更新任务签到信息")
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateTaskSignIn")
	public DbResponse addOrUpdateTaskSignIn(@RequestBody TaskSignIn taskSignIn,
											HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (taskSignIn==null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "参数不能为空！");
		}
		if (StringUtils.isEmpty(taskSignIn.getWorkOrderId())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "工单id不能为空！");
		}
		if (StringUtils.isEmpty(taskSignIn.getProcessInstanceId())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "流程实例id" +
					"不能为空！");
		}
		if (StringUtils.isEmpty(taskSignIn.getSignInUserId())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "签到人id不能为空！");
		}
		if (StringUtils.isEmpty(taskSignIn.getLongitudeLatitude())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "签到经纬度不能为空！");
		}
		if (StringUtils.isEmpty(taskSignIn.getSignInAddress())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"签到详细地址不能为空！");
		}
		try {
			taskSignIn.setTenantId(tenantId);
			return taskSignInService.addOrUpdateTaskSignIn(taskSignIn);
		} catch (Exception e) {
			log.error("签到异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "签到异常："+e);
		}
	}

}
